package bdddaosimpl;

import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import bdd.beans.DBLink;
import bdd.beans.Indisponibilites;
import bdd.beans.Jours;
import bdddaos.DAOJours;
import bdddaos.DAOJours;

public class DAOJoursImpl implements DAOJours {
	private DBLink dblink;

	public DAOJoursImpl() {		
		super();
		this.dblink = new DBLink(); 
		this.dblink.connexion();
	}

	@Override
	public Jours save(Jours o) {
		Jours retour = o;
		try {
			this.dblink.connexion();
			String r = "INSERT INTO JOURS (IDDATE)" +
			" VALUES ("+o.getIddate()+ ")";
			this.dblink.setPst(this.dblink.getConnection().prepareStatement(r));
			this.dblink.getPst().executeQuery();
		}
		catch(SQLException se) {
			System.out.println("Erreur de requ�te : " + se);
		}
		finally{
			this.dblink.deconnexion();
		}
		return retour;
	}

	@Override
	public Jours find(Date iddate) {
		Jours retour = new Jours();
		retour.setIddate(iddate);
		try {
			this.dblink.connexion();
			String r = "SELECT * FROM JOURS WHERE IDDATE = " + iddate;
			this.dblink.setPst(this.dblink.getConnection().prepareStatement(r));
			ResultSet rs = this.dblink.getPst().executeQuery();
			while(rs.next()) {
				/*DAOCreneauxImpl daoCreneaux = new DAOCreneauxImpl();
				retour.setCreneaus(daoCreneaux.find(rs.getInt("IDDATE")));		
				DAOReunionsImpl daoReunions = new DAOReunionsImpl();
				retour.setReunions(daoReunions.find(rs.getInt("IDDATE")));		
				DAOExamensImpl daoExamens = new DAOExamensImpl();
				retour.setExamens(daoExamens.find(rs.getInt("IDDATE")));		
				DAOExamensImpl daoExamens = new DAOExamensImpl();
				retour.set(daoExamens.find(rs.getInt("IDDATE")));		
				DAOExamensImpl daoExamens = new DAOExamensImpl();
				retour.setExamens(daoExamens.find(rs.getInt("IDDATE")));*/		
			}		
		}
		catch(SQLException se) {
			System.out.println("Erreur de requ�te" + se);
		}
		finally{
			this.dblink.deconnexion();
		}
		return retour;
	}

	@Override
	public List<Jours> findAll() {
		List<Jours> Lstretour = new ArrayList();
		try {
			this.dblink.connexion();
			this.dblink.setPst(this.dblink.getConnection().prepareStatement("SELECT * FROM JOURS"));
			ResultSet rs = this.dblink.getPst().executeQuery();
			while(rs.next()) {
				/*Indisponibilites retour = new Indisponibilites();
				retour.setDegre(rs.getInt("DEGRE"));
				retour.setHoraire(rs.getInt("HORAIRE"));
				retour.setJours(rs.getInt("JOURS"));
				//DAOEnseignantImpl daoEnseignant = new DAOEnseignantImpl();
				//retour.setEnseignant(daoEnseignant.find(rs.getInt("NUMENSEIGNANT")));
				Lstretour.add(retour);*/
			}		
		}
		catch(SQLException se) {
			System.out.println("Erreur de requ�te" + se);
		}
		finally{
			this.dblink.deconnexion();
		}
		return Lstretour;
	}

	@Override
	public void update(Jours o) {
		try {
			this.dblink.connexion();
			String r = "UPDATE JOURS SET IDDATE='"+o.getIddate()+ "' WHERE IDDATE="+o.getIddate();
			
			this.dblink.setPst(this.dblink.getConnection().prepareStatement(r));
			this.dblink.getPst().executeQuery();
		}
		catch(SQLException se) {
			System.out.println("Erreur de requ�te : " + se);
		}
		finally{
			this.dblink.deconnexion();
		}
	}

	@Override
	public void delete(Jours o) {
		try {
			this.dblink.connexion();
			String r = "DELETE FROM JOURS WHERE IDDATE="+o.getIddate();
			
			this.dblink.setPst(this.dblink.getConnection().prepareStatement(r));
			this.dblink.getPst().executeQuery();
		}
		catch(SQLException se) {
			System.out.println("Erreur de requ�te : " + se);
		}
		finally{
			this.dblink.deconnexion();
		}
	}

}
